﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Graphics;
using Microsoft.Xna.Framework;
using System.Linq;
using matrix = Microsoft.Xna.Framework.Matrix;
using color=Microsoft.Xna.Framework.Color ;

namespace XnaCanvas {
    public class XSence {
        public static GraphicsDevice Device;
        public color diffuseColor;
        static XSence() {
            if (GraphicsDeviceManager.Current != null)
                Device = GraphicsDeviceManager.Current.GraphicsDevice;
        }
        Dictionary<string, XObject> xobjs;
        public XObject this[string objectid] {
            get { return this.xobjs[objectid]; }
        }
        public T Get<T>(string id) where T : XObject {
            return xobjs[id] as T;
        }
        #region camera
        public matrix viewer;
        public matrix projection;
        //new Vector3(0, 0, 8f), Vector3.Zero, Vector3.Up, (float)Math.PI / 3f, 800f / 600f, 1, 100
        public static Vector3 camerap;
        public void InitialCamera(float cameraPos = 10, float fieldOfView = MathHelper.PiOver4, float aspectRedio = 1, float nearPlaneDistance = 1, float farPlaneDistance = 100) {
            //Vector3 cameraPosition, Vector3 cameraTarget,Vector3 cameraUpVector
            this.viewer = matrix.CreateLookAt(Vector3.Backward * cameraPos, Vector3.Zero, Vector3.Up);
            this.projection = matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRedio, nearPlaneDistance, farPlaneDistance);            
        }
        #endregion
        public XSence() {
            this.xobjs = new Dictionary<string, XObject>();
            InitialCamera();
            diffuseColor = color.FromNonPremultiplied(50,50,100,220);
        }
        public event EventHandler<RefreshStateArgs> RefreshState;
        protected virtual void OnRefreshState() {
            if (RefreshState != null) {
                RefreshState.Invoke(this, new RefreshStateArgs(xobjs));
            }
        }
        internal void Apply() {
            foreach (var item in xobjs) {
                item.Value.Present();
            }
            OnRefreshState();
        }
        public XObject Add(string id, XObject xo) {
            this.xobjs.Add(id, xo);
            xo.xsence = this;
            xo.SetData();
            return xo;
        }
        public XObject Add(XObject xo) {
            if (xo == null) return null;
            this.xobjs.Add(xo.GetType().Name, xo);
            xo.xsence = this;
            xo.SetData();
            return xo;
        }
        public void Remove(string id) {
            this.xobjs.Remove(id);
        }
        public void Clear() { this.xobjs.Clear(); }

        //internal void Scenery(Microsoft.Xna.Framework.Color color) {
        //    Device.Clear(color);
        //}
    }
    public class RefreshStateArgs : EventArgs {
        public RefreshStateArgs(Dictionary<string, XObject> r) {
            result = r;
        }
        public Dictionary<string, XObject> result;
    }
}
